﻿// ***************************************************************
//  Version: 0.4.0   			Date: 12/25/2007-2008
//	Auther: Deng Yangjun Email: dyj057@gmail.com  	
//  -------------------------------------------------------------
//  ORM静态工厂类库操作
//  -------------------------------------------------------------
//  (C)2007-2008 Midapex All Rights Reserved.
// ***************************************************************
//
// ***************************************************************
using System;
using System.Collections.Generic;
using System.Text;

namespace Midapex.SmartOrm
{
    public static class Orm
    {
        /// <summary>
        /// 生成表格
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        public static void CreateTable<T>()
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    orm.CreateTable<T>();
                    orm.CommitTrans();
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }

        }
            
        /// <summary>
        /// 生成表格
        /// </summary>
        /// <param name="type">实体类型</param>
        public static void CreateTable(Type type)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    orm.CreateTable(type);
                    orm.CommitTrans();
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 删除制定条件的对象
        /// </summary>
        /// <param name="type">实体类型</param>
        /// <param name="query">删除条件</param>
        /// <returns>返回删除的对象数目</returns>
        public static int Delete(Type type, Query query)
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    int count = orm.Delete(type,query);
                    orm.CommitTrans();
                    return count;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 删除子对象
        /// </summary>
        /// <param name="parent">父对象</param>
        /// <param name="child">子对象</param>
        public static void Delete(object parent, object child)
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    orm.Delete(parent,child);
                    orm.CommitTrans();
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }
        
        /// <summary>
        /// 删除对象和与它关联的子对象
        /// </summary>
        /// <param name="target">目标对象</param>
        /// <returns>删除成功返回true,否则返回false</returns>
        public static bool Delete(object target)
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result = orm.Delete(target);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 删除制定条件的对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="query">删除条件</param>
        /// <returns></returns>
        public static int Delete<T>(Query query)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    int count = orm.Delete<T>(query);
                    orm.CommitTrans();
                    return count;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }
        
        /// <summary>
        /// 删除数据表
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <returns>删除成功返回true,反则返回false</returns>
        public static bool DeleteTable<T>()
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result  = orm.DeleteTable<T>();
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 删除数据表
        /// </summary>
        /// <param name="type">实体类型</param>
        /// <returns>删除成功返回true,反则返回false</returns>
        public static bool DeleteTable(Type type)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result  = orm.DeleteTable(type);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 对象是否存在
        /// </summary>
        /// <param name="target">目标对象</param>
        /// <returns>存在返回true,否则返回false</returns>
        public static bool IsObjectExist(object target)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result  = orm.IsObjectExist(target);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 对象是否存在
        /// </summary>
        /// <param name="type">目标实体类型</param>
        /// <param name="idValue">对象的主键字段值</param>
        /// <returns>存在返回true,否则返回false</returns>
        public static bool IsObjectExist(Type type, object idValue)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result  = orm.IsObjectExist(type,idValue);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 对象是否存在
        /// </summary>
        /// <typeparam name="T">目标实体类型</typeparam>
        /// <param name="idValue">象的主键字段值</param>
        /// <returns>存在返回true,否则返回false</returns>
        public static bool IsObjectExist<T>(object idValue)
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result  = orm.IsObjectExist<T>(idValue);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 判断数据表是否存在
        /// </summary>
        /// <param name="type">目标类型</param>
        /// <returns>存在返回true,否则返回false</returns>
        public static bool IsTableExist(Type type)
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result  = orm.IsTableExist(type);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }

        }

        /// <summary>
        /// 判断数据表是否存在
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <returns>存在返回true,否则返回false</returns>
        public static bool IsTableExist<T>()
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    bool result  = orm.IsTableExist<T>();
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 读取对象
        /// </summary>
        /// <typeparam name="T">实体对象类型</typeparam>
        /// <param name="idValue">实体对象Id</param>
        /// <returns>对象</returns>
        public static T Load<T>(object idValue)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    T result  = orm.Load<T>(idValue);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 读取对象
        /// </summary>
        /// <param name="type">实体对象类型</param>
        /// <param name="idValue">实体对象Id</param>
        /// <returns>对象</returns>
        public static object Load(Type type, object idValue)
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    object result  = orm.Load(type,idValue);
                    orm.CommitTrans();
                    return result;
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 读取该类型的所有对象
        /// </summary>
        /// <typeparam name="T">目标兑现类型</typeparam>
        /// <returns>目标对象数组</returns>
        public static T[] LoadAll<T>()
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                return orm.LoadAll<T>();
            }
        }

        /// <summary>
        /// 重新创建所有的表格
        /// </summary>
        public static void RebuildAllTables()
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    orm.RebuildAllTables();
                    orm.CommitTrans();
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 保存子对象
        /// </summary>
        /// <param name="parent">父对象</param>
        /// <param name="child">子对象</param>
        public static void Save(object parent, object child)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    orm.Save(parent,child);
                    orm.CommitTrans();
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="target">目标对象</param>
        public static void Save(object target)
        {
             using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    orm.Save(target);
                    orm.CommitTrans();
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        /// <summary>
        /// 查询对象
        /// </summary>
        /// <typeparam name="T">目标对象类型</typeparam>
        /// <param name="query">查询条件</param>
        /// <returns>对象数组，如果没有查询到，返回空数组</returns>
        public static T[] Select<T>(Query query)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                return orm.Select<T>(query);
            }
        }

        /// <summary>
        /// 查询对象
        /// </summary>
        /// <param name="type">目标对象类型</param>
        /// <param name="query">查询条件</param>
        /// <returns>对象数组，如果没有查询到，返回空数组</returns>
        public static Array Select(Type type, Query query)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                return orm.Select(type, query);
            }
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="target">需要更新的对象</param>
        public static void Update(object target)
        {
            using (OrmSession orm = OrmSessionsFactory.CreateSession())
            {
                try
                {
                    orm.BeginTrans();
                    orm.Update(target);
                    orm.CommitTrans();
                }
                catch
                {
                    orm.RollbackTrans();
                    throw;
                }
            }
        }

        public static string QuoteName(string name)
        {
            using (OrmSession session = OrmSessionsFactory.CreateSession())
            {
                return session.QuoteName(name);
            }
        }

        public static string QuoteParam(string name)
        {
            using (OrmSession session = OrmSessionsFactory.CreateSession())
            {
                return session.QuoteParam(name);
            }
        }
        
    }
}
